package com.wwl.deliveryappointmentbackend.interceptor;



import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if (uri.equals("sys/users/login")) {
            // 如果是登录接口的请求，则放行，不进行登录状态的检查
            return true;
        }
        // 检查会话中是否存在用户信息，判断用户是否已经登录
        Object user = request.getSession().getAttribute("user");
        if (user == null) {
            // 用户未登录，重定向到登录页面或返回错误信息
            response.sendRedirect("/login");
            return false; // 停止继续执行
        }
        return true; // 用户已登录，继续执行请求
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 这里可以对响应进行处理，例如添加公共数据
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 请求处理完成后的清理工作
    }
}
